<div class="content-box"><!-- Start Content Box -->
    <div class="content-box-header">

        <h3>Sửa sản phẩm</h3>

        <ul class="content-box-tabs">

            <li><a href="#">Table</a></li> <!-- href must be unique and match the id of target div -->
            <li><a href="#tab2" class="default-tab">Forms</a></li>
        </ul>

        <div class="clear"></div>

    </div> <!-- End .content-box-header -->

    <div class="content-box-content">



        <div class="content-box-tabs" id="tab2">    <?php
            $sanpham='';$anhsp='';$gia='';$giaohang='';$tinhtrang='';$mota='';
            $noibat='';$muanhieu='';$danhmuc_id='';$chungloai_id='';$loai_id='';

            $hide_show=1;

            if(isset($_POST['sanpham']))			{$sanpham=$_POST['sanpham'];$hide_show=0;}
            if(isset($_POST['anhsp']))			    $anhsp=$_POST['anhsp'];
            if(isset($_POST['gia']))		        $gia=$_POST['gia'];
            if(isset($_POST['giaohang']))	        $giaohang=$_POST['giaohang'];
            if(isset($_POST['tinhtrang']))			$tinhtrang=$_POST['tinhtrang'];
            if(isset($_POST['mota']))			    $mota=$_POST['mota'];
            if(isset($_POST['noibat']))		        $noibat=$_POST['noibat'];
            if(isset($_POST['muanhieu']))			$muanhieu=$_POST['muanhieu'];
            if(isset($_POST['danhmuc_id']))			$danhmuc_id=$_POST['danhmuc_id'];
            if(isset($_POST['chungloai_id']))		$chungloai_id=$_POST['chungloai_id'];
            if(isset($_POST['loai_id']))		    $loai_id=$_POST['loai_id'];

            $id='';
            if(isset($_GET['id']))
                $id =$_GET['id'];

            if($hide_show==1)
            {
            $sql="SELECT * FROM tbl_sanpham WHERE id='".$id."'";
                $res=mysql_query($sql);
                $row=mysql_fetch_array($res);
                ?>
                <!-- <h1>Edit: </h1>-->
                <form action="" method="post" enctype="multipart/form-data">
                    <fieldset>
                        <div class="form1" style="width:500px; float:left; padding-left:0px">

                            <p>
                                <label>Tên sản phẩm</label>
                                <input autofocus  name="sanpham"  style="width:300px" value="<?php echo $row['sanpham'];?>"  type="text" placeholder="Tên sản phẩm"/><br />
                            </p>


                            <p>
                                <label>Ảnh sản phẩm</label>
                                <input  type="file" id="anhsp" name="anhsp"/>
                            </p>


                            <p>
                                <label>Giá</label>
                                <input name="gia"  style="width:300px"  value="<?php echo $row['gia'];?>" type="text" placeholder="Nhập giá(VNĐ)"/><br />
                            </p>
                            <p>
                                <label>Giao hàng</label>
                                <input name="giaohang"  style="width:300px"  value="<?php echo $row['giaohang'];?>" type="text" placeholder=""/><br />
                            </p>

                            <p>
                                <label>Tình trạng</label>
                                <select  name="tinhtrang" style="width:300px">
                                    <option value="1" <?php if($row['tinhtrang']==1)echo 'selected';?>>Còn hàng</option>
                                    <option value="0" <?php if($row['tinhtrang']==0)echo 'selected';?>>Hết hàng</option>
                                </select>
                            </p>
                        </div>
                        <div class="form2" style="float:left; width:500px; padding-bottom:20px">
                            <p>

                                <label for="noibat">Sản phẩm nổi bật</label>
                                <input type="checkbox" <?php if($row['noibat']==1)echo 'checked';?> name="noibat" id="noibat" value="1" />

                                <label for="muanhieu">Sản phẩm mua nhiều</label>
                                <input type="checkbox" <?php if($row['muanhieu']==1)echo 'checked';?> name="muanhieu" id="muanhieu" value="1" />
                                <br />
                            </p>
                            <p>
                                <label for="danhmuc_id">Tên danh mục sản phẩm</label>
                                <?php
                                $sql1='SELECT      tbl_danhmuc.danhmuc,
                                                   tbl_danhmuc.danhmuc_id
                                                   FROM tbl_danhmuc WHERE danhmuc_id="'.$row['danhmuc_id'].'"';
                                $result1=mysql_query($sql1);
                                $row1=mysql_fetch_array($result1);
                                ?>
                                <select name="danhmuc_id" id="danhmuc_id" style="width:305px">
                                    <option value="<?php echo $row1['danhmuc_id'];?>"><?php echo $row1['danhmuc']?></option>
                                    <?php
                                    $sql2='SELECT      tbl_danhmuc.danhmuc,
                                                   tbl_danhmuc.danhmuc_id
                                                   FROM tbl_danhmuc WHERE danhmuc_id!="'.$row['danhmuc_id'].'"';
                                    $result2=mysql_query($sql2);
                                    while($row2=mysql_fetch_array($result2))
                                    {
                                        ?>
                                        <option value="<?php echo $row2['danhmuc_id'];?>"><?php echo $row2['danhmuc']?></option>
                                    <?php
                                    }
                                    ?>
                                </select>
                                <strong style="width:130px;padding: 4px 0px; background:#005580; border-radius: 3px; text-align:center">
                                    <a href="index.php?page=danhmuc&act=add" style=" color:#FFF; padding:10px;font-size:12px">Thêm danh muc</a>
                                </strong>
                            </p>
                            <div class="chungloai_id">
                                <p>
                                    <label for="chungloai_id">Chủng loại sản phẩm</label>
                                    <?php
                                    $sql3='SELECT      tbl_chungloai.chungloai,
                                                   tbl_chungloai.chungloai_id
                                                   FROM tbl_chungloai WHERE chungloai_id="'.$row['chungloai_id'].'"';
                                    $result3=mysql_query($sql3);
                                    $row3=mysql_fetch_array($result3);
                                    ?>
                                    <select name="chungloai_id" id="chungloai_id" style="width:305px">
                                        <option value="<?php echo $row3['chungloai_id'];?>"><?php echo $row3['chungloai']?></option>
                                        <?php
                                        $sql4='SELECT      tbl_chungloai.chungloai,
                                                   tbl_chungloai.chungloai_id
                                                   FROM tbl_chungloai WHERE chungloai_id!="'.$row['chungloai_id'].'"';
                                        $result4=mysql_query($sql4);
                                        while($row4=mysql_fetch_array($result4))
                                        {
                                            ?>
                                            <option value="<?php echo $row4['chungloai_id'];?>"><?php echo $row4['chungloai']?></option>
                                        <?php
                                        }
                                        ?>
                                    </select>
                                    <strong style="width:130px;padding: 4px 0px; background:#005580; border-radius: 3px; text-align:center">
                                        <a href="index.php?page=chungloai&act=add" style=" color:#FFF; padding:10px;font-size:12px">Thêm chủng loại</a>
                                    </strong>
                                </p>
                                <div class="loai_id">
                                    <p>
                                        <label for="loai_id">Loại sản phẩm</label>
                                        <?php
                                        $sql5='SELECT      tbl_loai.loai,
                                                   tbl_loai.loai_id
                                                   FROM tbl_loai WHERE loai_id="'.$row['loai_id'].'"';
                                        $result5=mysql_query($sql5);
                                        $row5=mysql_fetch_array($result5);
                                        ?>
                                        <select name="loai_id" id="loai_id" style="width:305px">
                                            <option value="<?php echo $row5['loai_id'];?>"><?php echo $row5['loai']?></option>
                                            <?php
                                            $sql6='SELECT      tbl_loai.loai,
                                                                tbl_loai.loai_id
                                                   FROM tbl_loai WHERE loai_id!="'.$row['loai_id'].'"';
                                            $result6=mysql_query($sql6);
                                            while($row6=mysql_fetch_array($result6))
                                            {
                                                ?>
                                                <option value="<?php echo $row6['loai_id'];?>"><?php echo $row6['loai']?></option>
                                            <?php
                                            }
                                            ?>
                                        </select>
                                        <strong style="width:130px;padding: 4px 0px; background:#005580; border-radius: 3px; text-align:center">
                                            <a href="index.php?page=loai&act=add" style=" color:#FFF; padding:29px;font-size:12px">Thêm loại</a>
                                        </strong>
                                    </p>
                                </div>
                            </div>
                        </div>
                        <p style="width:1000px; ">
                            <label>Thông tin chi tiết * <a href="#" style="color: #117700">(Kích thước chiều rộng tối đa là 560px !)</a></label>
                            <textarea name='mota' class='ckeditor' rows='10' cols='10' ><?php echo $row['mota'];?></textarea>
                        </p>
                    </fieldset>
                    <p style="padding-top:50px">
                        <input class="btn" type="reset" value="Nhập lại"  style=" width:100px;padding: 4px 0px;  border-radius:5px;"/>
                        <input class="btn" type="submit" value="Cập nhật" name="sua" style=" width:200px;padding: 4px 0px; background:#069; color:#FFF; border-radius:5px;"/>

                    </p>
                    <div class="clear"></div><!-- End .clear -->

                </form>

            <?php
            }
            else{
                //Define some variables
                $dir = "uploads/"; //Bạn nên thay đổi đường dẫn cho phù hợp
                //Kiều file, Gif, jpeg, zip ::bạn có thể sửa đổi nếu thích
                $types = array("image/gif","image/GIF","image/JPG","image/PNG","image/png","image/jpg","image/JPEG","image/jpeg","application/x-zip-compressed","application/msword","application/vnd.ms-excel");
                //Check to determine if the submit button has been pressed
                if(isset($_POST["sua"])){
                    //Shorten Variables
                    $tmp_name = $_FILES["anhsp"]["tmp_name"];
                    $new_name = $_FILES["anhsp"]["name"];
                    //Check MIME Type
                    if (in_array($_FILES["anhsp"]["type"], $types)){
                        //Move file from tmp dir to new location
                        move_uploaded_file($tmp_name,'../'.$dir.$new_name);
                        $anhsp=$new_name;
                        echo "<script>alert('File {$_FILES["anhsp"]["name"]} Up load ảnh thành công!');</script>";
                    }else{
                        //Print Error Message
                        echo "<script>alert('{$_FILES["anhsp"]["name"]} Bạn chưa upload hình ảnh cho sản phẩm !');</script>";
                        //Debug
                        $name =  $_FILES["anhsp"]["name"];
                        $type =  $_FILES["anhsp"]["type"];
                        $size =  $_FILES["anhsp"]["size"];
                        $tmp  =  $_FILES["anhsp"]["name"];

                    }
                }
                else{
                    echo 'Không có file upload';
                }

                if($anhsp=='')
                {
                    $sql="UPDATE 	tbl_sanpham
							SET
									sanpham='".$sanpham."',
									gia='".$gia."',
									giaohang='".$giaohang."',
									tinhtrang='".$tinhtrang."',
									mota='".$mota."',
									noibat='".$noibat."',
									muanhieu='".$muanhieu."',
									danhmuc_id='".$danhmuc_id."',
									chungloai_id='".$chungloai_id."',
									loai_id='".$loai_id."'

							WHERE id='".$id."'";
                    $result=mysql_query($sql);
                    //header('refresh:1; index.php?page=product&act=product_add');

                }else
                {
                    $sql="UPDATE 	tbl_sanpham
							SET
									sanpham='".$sanpham."',
									anhsp='".$anhsp."',
									gia='".$gia."',
									giaohang='".$giaohang."',
									tinhtrang='".$tinhtrang."',
									mota='".$mota."',
									noibat='".$noibat."',
									muanhieu='".$muanhieu."',
									danhmuc_id='".$danhmuc_id."',
									chungloai_id='".$chungloai_id."',
									loai_id='".$loai_id."'

							WHERE id='".$id."'";
                    $result=mysql_query($sql);
                }
                header('refresh:1; index.php?page=product');
            }
            ?>
        </div> <!-- End #tab2 -->
        <div class="tab-content" id="tab1"> <!-- This is the target div. id must match the href of this div's tab -->
        </div> <!-- End #tab1 -->
    </div> <!-- End .content-box-content -->
</div> <!-- End .content-box -->